package com.tgb.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.tgb.entity.Teacher;

@Repository
public class TeacherDaoImpl implements TeacherDao {  
	@Resource(name="sessionFactory")
    private SessionFactory sessionFactory;  
  
    public void setSessionFactory(SessionFactory sessionFactory) {  
        this.sessionFactory = sessionFactory;  
    }

	@Override
	public Teacher getTeacher(String id) {
        String hql = "from Teacher t where t.id=?";  
        Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        query.setString(0, id);  
          
        return (Teacher)query.uniqueResult();
	}

	@Override
	public List<Teacher> getAllTeacher() {
		String hql = "from Teacher";  
        Query query = sessionFactory.getCurrentSession().createQuery(hql);  
          
        return query.list();
	}

	@Override
	public void addTeacher(Teacher teacher) {
		sessionFactory.getCurrentSession().save(teacher);  
		
	}

	@Override
	public boolean delTeacher(String id) {
		String hql = "delete Teacher t where t.id= ?";  
        Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        query.setString(0, id);  
          
        return (query.executeUpdate() > 0);  
	}

	@Override
	public boolean updateTeacher(Teacher teacher) {
		String hql = "update Teacher t set t.name = ? where t.id = ?";  
        Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        query.setString(0, teacher.getName());  
        query.setString(1, teacher.getId());   
          
        return (query.executeUpdate() > 0); 
	}  
      
}